package com.ruoyi.utils;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.Map;

public class HttpUtils {

    private static final HttpClient httpClient = HttpClient.newBuilder()
            .connectTimeout(Duration.ofSeconds(10)) // 设置连接超时
            .build();

    private static final ObjectMapper objectMapper = new ObjectMapper(); // 用于处理JSON

/*
*
     * 发送GET请求
     * @param url 请求的URL地址
     * @param headers 请求头的Map集合
     * @return 返回响应的结果，JSON格式
     * @throws Exception 请求失败时抛出异常
*/
    public static String sendGet(String url, Map<String, String> headers) throws Exception {
        // 创建GET请求
        HttpRequest.Builder requestBuilder = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .timeout(Duration.ofSeconds(10)) // 请求超时时间
                .GET();

        // 设置请求头
        if (headers != null) {
            headers.forEach(requestBuilder::header);
        }

        HttpRequest request = requestBuilder.build();

        // 发送请求并获取响应
        HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());

        // 判断响应状态码
        if (response.statusCode() == 200) {
            return response.body(); // 返回响应体
        } else {
            throw new RuntimeException("GET请求失败，状态码: " + response.statusCode());
        }
    }

/*
*
     * 发送POST请求
     * @param url 请求的URL地址
     * @param headers 请求头的Map集合
     * @param requestBody 请求体，可以是JSON格式的字符串
     * @return 返回响应的结果，JSON格式
     * @throws Exception 请求失败时抛出异常
*/
    public static String sendPost(String url, Map<String, String> headers, String requestBody) throws Exception {
        // 创建POST请求
        HttpRequest.Builder requestBuilder = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .timeout(Duration.ofSeconds(10)) // 请求超时时间
                .POST(HttpRequest.BodyPublishers.ofString(requestBody)); // 设置请求体

        // 设置请求头
        if (headers != null) {
            headers.forEach(requestBuilder::header);
        }

        HttpRequest request = requestBuilder.build();

        // 发送请求并获取响应
        HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());

        // 判断响应状态码
        if (response.statusCode() == 200) {
            return response.body(); // 返回响应体
        } else {
            throw new RuntimeException("POST请求失败，状态码: " + response.statusCode());
        }
    }

/*
*
     * 将对象转换为JSON字符串
     * @param obj 需要转换的对象
     * @return 对象的JSON格式字符串
     * @throws Exception 转换失败时抛出异常
*/
    public static String convertObjectToJson(Object obj) throws Exception {
        return objectMapper.writeValueAsString(obj);
    }

/*
*
     * 将JSON字符串转换为指定类型的对象
     * @param json JSON字符串
     * @param clazz 转换后的对象类型
     * @param <T> 泛型
     * @return 转换后的对象
     * @throws Exception 转换失败时抛出异常
*/
    public static <T> T convertJsonToObject(String json, Class<T> clazz) throws Exception {
        return objectMapper.readValue(json, clazz);
    }
}
